//
//  CategoryAddViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 31.03.25.
//

import Foundation
class CategoryAddViewModel : KoiAccountingBaseViewModel {
    
    @Published var linearGroupTitles = CategoryIconLocalData.linearGroupTitles()
    @Published var emojiGroupTitles = CategoryIconLocalData.emojiGroupTitles()
    
    @Published var linearSelectedGroupTitle: CategoryGroupTitle? = nil
    @Published var emojiSelectedGroupTitle: CategoryGroupTitle? = nil
    
    @Published var linearCategoryIconList = Array<CategoryIcon>()
    @Published var emojiCategoryIconList = Array<CategoryIcon>()
    
    @Published var createCategoryState = ""
    @Published var updateCategoryState = ""
    
    private let categoryDao = KoiAccountingDB.standard.getCategoryDao()
    
    func saveCategory(name: String, icon: String, type: String, parentId: String? = nil) {
        let now = Date()
        let createTime = now.toISODateString()
        var category = AccountingCategory()
        category.name = name
        category.bookId = bookId
        category.uid = uid
        category.icon = icon
        category.type = type
        category.parentId = parentId
        category.createTime = createTime
        category.updateTime = createTime
        DispatchQueue.global(qos: .background).async {
            self.categoryDao.save(category: category)
            DispatchQueue.main.async {
                self.createCategoryState = "success"
            }
        }
    }
    
    func updateCategory(category: AccountingCategory?, name: String, icon: String) {
        guard var category = category else { return }
        category.name = name
        category.icon = icon
        asyncBackground {
            self.categoryDao.update(category:category)
            DispatchQueue.main.async {
                self.updateCategoryState = "success"
            }
        }
    }
    
    func searchCategory(categoryQuery: String) {
        // 查询 分别查询 linear 数据和 emoji 数据
        if (categoryQuery.isEmpty) {
            linearDefaultSelectedData()
            emojiDefaultSelectedData()
        } else {
            // 查找
            let linearResult = CategoryIconLocalData.searchLinear(query: categoryQuery)
            let emojiResult = CategoryIconLocalData.searchEmoji(query: categoryQuery)
            let linearResultTitles = CategoryIconLocalData.findGroupTitles(iconData: linearResult, type: .linearicon)
            let emojiResultTitles = CategoryIconLocalData.findGroupTitles(iconData: emojiResult, type: .emojiicon)
            
            linearGroupTitles = linearResultTitles
            emojiGroupTitles = emojiResultTitles
            
            linearSelectedGroupTitle = linearResultTitles.first
            emojiSelectedGroupTitle = emojiResultTitles.first
            
            linearCategoryIconList = CategoryIconLocalData.getSelectedIconList(title: linearSelectedGroupTitle, searchQuery: categoryQuery)
            emojiCategoryIconList = CategoryIconLocalData.getSelectedIconList(title: emojiSelectedGroupTitle, searchQuery: categoryQuery)
        }
    }
    
    func linearDefaultSelectedData() {
        linearGroupTitles = CategoryIconLocalData.linearGroupTitles()
        linearSelectedGroupTitle = linearGroupTitles.first
        let linearList = CategoryIconLocalData.linearMapList.first(where: { icon in
            icon.name == linearSelectedGroupTitle?.name
        })?.data
        linearCategoryIconList = linearList ?? Array()
    }
    
    func emojiDefaultSelectedData() {
        emojiGroupTitles = CategoryIconLocalData.emojiGroupTitles()
        emojiSelectedGroupTitle = emojiGroupTitles.first
        let emojiList = CategoryIconLocalData.emojiMapList.first(where: { icon in
            icon.name == emojiSelectedGroupTitle?.name
        })?.data
        emojiCategoryIconList = emojiList ?? Array()
    }
}
